package com.ddtv.video.videostreaming;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoStreamingActivity extends Activity {

	public final static String VIDEO_LINK="video_link";

	private VideoView videoView1;
	private Handler handler;
	private ProgressDialog dialog;
	private String link ="";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);       
		setContentView(R.layout.video_view);

		init();
		setupDefaults();
	} 

	private void init(){
		handler = new Handler();
		dialog = new ProgressDialog(this);
		dialog.setMessage("Loading Video...");
		dialog.setCancelable(true);

		handler.post(new Runnable() {						
			@Override
			public void run() {
				dialog.show();
			}
		});						 
	}

	private void setupDefaults(){		
		if(!isConnectionAvailable()){
			handler.post(new Runnable() {				
				@Override
				public void run() {
					if(dialog.isShowing()){
						dialog.dismiss();
					}
				}
			});
			
			Toast.makeText(this, "Internet Connection not available.", Toast.LENGTH_LONG).show();
			finish();
		}
		
		Bundle bundle = getIntent().getExtras();

		if(bundle!=null){		        			        			        	
				link = bundle.getString(VIDEO_LINK);		        	
				this.videoView1 = (VideoView) findViewById(R.id.videoView1);
				videoView1.setVideoPath(link);
				videoView1.setMediaController(new MediaController(this));
				//videoView1.setKeepScreenOn(true);
				videoView1.setOnPreparedListener(prepared);
				videoView1.setOnCompletionListener(completed);
				videoView1.setOnErrorListener(error);
				videoView1.start();									
		}else{
			Toast.makeText(this, "Unable to play this video.", Toast.LENGTH_SHORT).show();
		}	        	
	}

	private OnPreparedListener prepared = new OnPreparedListener() {
		@Override
		public void onPrepared(MediaPlayer mp) {
			dialog.dismiss();
			videoView1.setKeepScreenOn(false);
		}
	};

	private OnCompletionListener completed = new OnCompletionListener() {		
		@Override
		public void onCompletion(MediaPlayer mp) {							
			mp.reset();
			videoView1.setVideoPath(link);
			videoView1.start();			
			videoView1.pause();
		}
	};

	private OnErrorListener error = new OnErrorListener() {			
		@Override
		public boolean onError(MediaPlayer mp, int what, int extra) {									
			if(dialog.isShowing())
				dialog.dismiss();
			Toast.makeText(VideoStreamingActivity.this, "Unable to play this video.", Toast.LENGTH_SHORT).show();
			VideoStreamingActivity.this.finish();			
			return true;
		}
	};
	
	
	private boolean isConnectionAvailable(){
		boolean status = false;
		
		ConnectivityManager connManager  = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo netInfo = connManager.getActiveNetworkInfo();
		
		if(netInfo!=null){
			status = netInfo.isConnected();
		}
		
		return  status;
	}

}